package com.kaikeba.dao.impl;

import com.kaikeba.bean.Express;
import com.kaikeba.dao.BaseExpressDao;
import com.kaikeba.exciption.CodeException;
import com.kaikeba.util.DruidUtil;

import java.sql.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ExpressDaoMysql implements BaseExpressDao {
    //用于查询数据库中的全部快递（总数 + 新增）
    public static final String SQL_CONSOLE = "SELECT COUNT(ID) data1_size,COUNT(TO_DAYS(INTIME)=TO_DAYS(NOW()) OR NULL) data1_day,COUNT(STATUS=0 OR NULL) data2_size,COUNT(TO_DAYS(INTIME)=TO_DAYS(NOW()) AND STATUS=0 OR NULL) data2_day FROM EXPRESS";
    //查询数据库中的所有快递信息
    public static final String SQL_FIND_ALL = "SELECT * FROM EXPRESS";
    //用于分页查询数据库中的快递信息
    public static final String SQL_FIND_LIMIT = "SELECT * FROM EXPRESS LIMIT ?,?";
    //通过取件码查询快递信息
    public static final String SQL_FIND_BY_CODE = "SELECT * FROM EXPRESS WHERE CODE=?";
    //通过快递单号查询快递信息
    public static final String SQL_FIND_BY_NUMBER = "SELECT * FROM EXPRESS WHERE NUMBER=?";
    //通过录入路人的手机号码查询快递信息
    public static final String SQL_FIND_BY_SYSPHONE = "SELECT * FROM EXPRESS WHERE SYSPHONE=?";
    //通过用户手机号查询用户所有快递
    public static final String SQL_FIND_BY_USERPHONE = "SELECT * FROM EXPRESS WHERE USERPHONE=?";
    //通过用户手机号查询用户所有快递
    public static final String SQL_FIND_BY_USERPHONE_AND_STATUS = "SELECT * FROM EXPRESS WHERE USERPHONE=? AND STATUS=?";
    //录入快递
    public static final String SQL_INSERT = "INSERT INTO EXPRESS(NUMBER,USERNAME,USERPHONE,COMPANY,CODE,INTIME,STATUS,SYSPHONE) VALUES(?,?,?,?,?,NOW(),0,?)";
    //快递修改
    public static final String SQL_UPDATE = "UPDATE EXPRESS SET NUMBER=?,USERNAME=?,COMPANY=?,STATUS=? WHERE ID=?";
    //快递的状态码的改变(取件)
    public static final String SQL_UPDATE_STATUS = "UPDATE EXPRESS SET STATUS=1,OUTTIME=NOW(),CODE=NULL WHERE CODE=?";
    //快递的删除
    public static final String SQL_DELETE = "DELETE FROM EXPRESS WHERE ID=?";
    //查询每个人总的快递数量与人名
    public static final String SQL_FINDALL_AND_NAME = "SELECT USERNAME,COUNT(USERNAME) SUM_EXPRESS FROM EXPRESS GROUP BY USERNAME ORDER BY SUM_EXPRESS DESC";
    //查询每个人当月的快递数量与人名
    public static final String SQL_FINDALL_AND_NAME_FROM_MONTH = "SELECT USERNAME,COUNT(USERNAME) MONTH_EXPRESS FROM EXPRESS WHERE DATE_FORMAT(INTIME,'%Y%m') = DATE_FORMAT(CURDATE(),'%Y%m') GROUP BY USERNAME ORDER BY MONTH_EXPRESS DESC";
    //查询每个人当年的快递数量与人名
    public static final String SQL_FINDALL_AND_NAME_FROM_YEAR = "SELECT USERNAME,COUNT(USERNAME) YEAR_EXPRESS FROM EXPRESS WHERE YEAR(INTIME) = YEAR(NOW()) GROUP BY USERNAME ORDER BY YEAR_EXPRESS DESC";


    /**
     * 用于查询数据库中的全部快递（总数 + 新增）
     * 待取件快递（总数 + 新增）
     *
     * @return [{size:总数,day:新增},{size:总数,day:新增}]
     */
    @Override
    public List<Map<String, Integer>> console() {
        ArrayList<Map<String,Integer>> data = new ArrayList<>();
        //1.  获取数据库的连接
        Connection conn = DruidUtil.getConnection();
        PreparedStatement state = null;
        ResultSet result = null;
        //2.  预编译SQL语句
        try {
            state = conn.prepareStatement(SQL_CONSOLE);
            //3.  填充参数(可选)

            //4.  执行SQL语句
            result = state.executeQuery();
            //5.  获取执行的结果
            if(result.next()){
                int data1_size = result.getInt("data1_size");
                int data1_day = result.getInt("data1_day");
                int data2_size = result.getInt("data2_size");
                int data2_day = result.getInt("data2_day");
                Map data1 = new HashMap();
                data1.put("data1_size",data1_size);
                data1.put("data1_day",data1_day);
                Map data2 = new HashMap();
                data2.put("data2_size",data2_size);
                data2.put("data2_day",data2_day);
                data.add(data1);
                data.add(data2);

            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            //6.  资源的释放
            DruidUtil.close(conn,state,result);
        }
        return data;
    }

    /**
     * 用于查询所有快递
     *
     * @param limit      是否分页标记，true表示分页，false表示查询所有快递
     * @param offset     SQL语句的起始索引
     * @param pageNumber 页查询的数量
     * @return 快递的集合
     */
    @Override
    public List<Express> findAll(boolean limit, int offset, int pageNumber) {
        ArrayList<Express> data = new ArrayList<>();
        //1.  获取数据库的连接
        Connection conn = DruidUtil.getConnection();
        PreparedStatement state = null;
        ResultSet result = null;
        //2.  预编译SQL语句
        try {
            if(limit) {
                state = conn.prepareStatement(SQL_FIND_LIMIT);
                //填充参数
                state.setInt(1,offset);
                state.setInt(2,pageNumber);
            }else{
                state = conn.prepareStatement(SQL_FIND_ALL);
            }
            //3.  填充参数(可选)

            //4.  执行SQL语句
            result = state.executeQuery();
            //5.  获取执行的结果
            while (result.next()){
                int id = result.getInt("id");
                String number = result.getString("number");
                String username = result.getString("username");
                String userPhone = result.getString("userPhone");
                String company = result.getString("company");
                String code = result.getString("code");
                Timestamp inTime = result.getTimestamp("inTime");
                Timestamp outTime = result.getTimestamp("outTime");
                int status = result.getInt("status");
                String sysPhone = result.getString("sysPhone");
                Express e = new Express(id,number,username,userPhone,company,code,inTime,outTime,status,sysPhone);
                data.add(e);
            }

        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            //6.  资源的释放
            DruidUtil.close(conn,state,result);
        }
        return data;
    }

    /**
     * 根据快递单号查询快递信息
     *
     * @param number 单号
     * @return 查询快递信息，单号不存在时返回null
     */
    @Override
    public Express findByNumber(String number) {
        //1.  获取数据库的连接
        Connection conn = DruidUtil.getConnection();
        PreparedStatement state = null;
        ResultSet result = null;
        //2.  预编译SQL语句
        try {
            state = conn.prepareStatement(SQL_FIND_BY_NUMBER);
            //3.  填充参数(可选)
            state.setString(1,number);
            //4.  执行SQL语句
            result = state.executeQuery();
            //5.  获取执行的结果
            if(result.next()){
                int id = result.getInt("id");
                String username = result.getString("username");
                String userPhone = result.getString("userPhone");
                String company = result.getString("company");
                String code = result.getString("code");
                Timestamp inTime = result.getTimestamp("inTime");
                Timestamp outTime = result.getTimestamp("outTime");
                int status = result.getInt("status");
                String sysPhone = result.getString("sysPhone");
                Express e = new Express(id,number,username,userPhone,company,code,inTime,outTime,status,sysPhone);
                return e;
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            //6.  资源的释放
            DruidUtil.close(conn,state,result);
        }
        return null;
    }

    /**
     * 根据快递取件码查询快递信息
     *
     * @param code 取件码
     * @return 查询快递信息，单号不存在时返回null
     */
    @Override
    public Express findByCode(String code) {
        //1.  获取数据库的连接
        Connection conn = DruidUtil.getConnection();
        PreparedStatement state = null;
        ResultSet result = null;
        //2.  预编译SQL语句
        try {
            state = conn.prepareStatement(SQL_FIND_BY_CODE);
            //3.  填充参数(可选)
            state.setString(1,code);
            //4.  执行SQL语句
            result = state.executeQuery();
            //5.  获取执行的结果
            if(result.next()){
                int id = result.getInt("id");
                String number = result.getString("number");
                String username = result.getString("username");
                String userPhone = result.getString("userPhone");
                String company = result.getString("company");
                Timestamp inTime = result.getTimestamp("inTime");
                Timestamp outTime = result.getTimestamp("outTime");
                int status = result.getInt("status");
                String sysPhone = result.getString("sysPhone");
                Express e = new Express(id,number,username,userPhone,company,code,inTime,outTime,status,sysPhone);
                return e;
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            //6.  资源的释放
            DruidUtil.close(conn,state,result);
        }
        return null;
    }

    /**
     * 根据用户手机号查询快递信息
     *
     * @param userPhone 手机号
     * @return 查询的快递信息列表
     */
    @Override
    public List<Express> findByUserPhone(String userPhone) {
        List<Express> data = new ArrayList<>();
        //1.  获取数据库的连接
        Connection conn = DruidUtil.getConnection();
        PreparedStatement state = null;
        ResultSet result = null;
        //2.  预编译SQL语句
        try {
            state = conn.prepareStatement(SQL_FIND_BY_USERPHONE);
            //3.  填充参数(可选)
            state.setString(1,userPhone);
            //4.  执行SQL语句
            result = state.executeQuery();
            //5.  获取执行的结果
            while (result.next()){
                int id = result.getInt("id");
                String number = result.getString("number");
                String username = result.getString("username");
                String company = result.getString("company");
                String code = result.getString("code");
                Timestamp inTime = result.getTimestamp("inTime");
                Timestamp outTime = result.getTimestamp("outTime");
                int status = result.getInt("status");
                String sysPhone = result.getString("sysPhone");
                Express e = new Express(id,number,username,userPhone,company,code,inTime,outTime,status,sysPhone);
                data.add(e);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            //6.  资源的释放
            DruidUtil.close(conn,state,result);
        }
        return data;
    }

    /**
     * 根据用户手机号.与状态码查询快递信息  这里一般查未取件的快递信息
     *
     * @param userPhone 手机号
     * @param status
     * @return 查询的快递信息列表
     */
    @Override
    public List<Express> findByUserPhoneAndStatus(String userPhone, int status) {
        List<Express> data = new ArrayList<>();
        //1.  获取数据库的连接
        Connection conn = DruidUtil.getConnection();
        PreparedStatement state = null;
        ResultSet result = null;
        //2.  预编译SQL语句
        try {
            state = conn.prepareStatement(SQL_FIND_BY_USERPHONE_AND_STATUS);
            //3.  填充参数(可选)
            state.setString(1,userPhone);
            state.setInt(2,status);
            //4.  执行SQL语句
            result = state.executeQuery();
            //5.  获取执行的结果
            while (result.next()){
                int id = result.getInt("id");
                String number = result.getString("number");
                String username = result.getString("username");
                String company = result.getString("company");
                String code = result.getString("code");
                Timestamp inTime = result.getTimestamp("inTime");
                Timestamp outTime = result.getTimestamp("outTime");
                String sysPhone = result.getString("sysPhone");
                Express e = new Express(id,number,username,userPhone,company,code,inTime,outTime,status,sysPhone);
                data.add(e);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            //6.  资源的释放
            DruidUtil.close(conn,state,result);
        }
        return data;
    }

    /**
     * 根据录入人的手机号码，查询录入的所有记录
     *
     * @param sysPhone 手机号码
     * @return 查询的快递信息列表
     */
    @Override
    public List<Express> findBySysPhone(String sysPhone) {
        List<Express> data = new ArrayList<>();
        //1.  获取数据库的连接
        Connection conn = DruidUtil.getConnection();
        PreparedStatement state = null;
        ResultSet result = null;
        //2.  预编译SQL语句
        try {
            state = conn.prepareStatement(SQL_FIND_BY_SYSPHONE);
            //3.  填充参数(可选)
            state.setString(1,sysPhone);
            //4.  执行SQL语句
            result = state.executeQuery();
            //5.  获取执行的结果
            while (result.next()){
                int id = result.getInt("id");
                String number = result.getString("number");
                String username = result.getString("username");
                String company = result.getString("company");
                String userPhone = result.getString("userPhone");
                String code = result.getString("code");
                Timestamp inTime = result.getTimestamp("inTime");
                Timestamp outTime = result.getTimestamp("outTime");
                int status = result.getInt("status");
                Express e = new Express(id,number,username,userPhone,company,code,inTime,outTime,status,sysPhone);
                data.add(e);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            //6.  资源的释放
            DruidUtil.close(conn,state,result);
        }
        return data;
    }

    /**
     * 快递录入
     *(NUMBER,USERNAME,USERPHONE,COMPANY,CODE,INTIME,STATUS,SYSPHONE) VALUES(?,?,?,?,?,NOW(),0,?)
     * @param e 要录入的快递对象
     * @return 录入的结果，true表示成功，false表示失败
     */
    @Override
    public boolean insert(Express e) throws CodeException{
        // 1.链接的获取
        Connection conn = DruidUtil.getConnection();
        // 2.预编译SQL语句
        PreparedStatement state = null;
        try {
            state = conn.prepareStatement(SQL_INSERT);
            // 3.填充参数
            state.setString(1,e.getNumber());
            state.setString(2,e.getUsername());
            state.setString(3,e.getUserPhone());
            state.setString(4,e.getCompany());
            state.setString(5,e.getCode());
            state.setString(6,e.getSysPhone());
            // 4.执行SQL语句
            return state.executeUpdate()>0?true:false;
        } catch (SQLException e1) {
            if(e1.getMessage().endsWith("for key 'code'")){
                //取件码重复，出现了异常
                CodeException e2 = new CodeException(e1.getMessage());
                throw e2;
            }
        }finally {
            // 5.释放资源
            DruidUtil.close(conn,state,null);
        }
        return false;
    }

    /**
     * 快递的修改
     *"UPDATE EXPRESS SET NUMBER=?,USERNAME=?,COMPANY=?,STATUS=? WHERE ID=?";
     * @param id         要修改的快递id
     * @param newExpress 新的快递对象（number,company,username,userPhone）
     * @return 修改的结果，true表示成功，false表示失败
     */
    @Override
    public boolean update(int id, Express newExpress) {
        // 1.链接的获取
        Connection conn = DruidUtil.getConnection();
        // 2.预编译SQL语句
        PreparedStatement state = null;
        try {
            state = conn.prepareStatement(SQL_UPDATE);
            state.setString(1,newExpress.getNumber());
            state.setString(2,newExpress.getUsername());
            state.setString(3,newExpress.getCompany());
            state.setInt(4,newExpress.getStatus());
            state.setInt(5,id);
            return state.executeUpdate()>0?true:false;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            DruidUtil.close(conn,state,null);
        }
        return false;
    }

    /**
     * 跟该快递的状态为1，表示取件完成
     *
     * @param code 取件码
     * @return 修改的结果，true表示成功，false表示失败
     */
    @Override
    public boolean updateStatus(String code) {
        // 1.链接的获取
        Connection conn = DruidUtil.getConnection();
        // 2.预编译SQL语句
        PreparedStatement state = null;
        try {
            state = conn.prepareStatement(SQL_UPDATE_STATUS);
            state.setString(1,code);
            return state.executeUpdate()>0?true:false;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            DruidUtil.close(conn,state,null);
        }
        return false;
    }

    /**
     * 根据id，删除单个快递信息
     *
     * @param id 要删除的快递id
     * @return 删除的结果，true表示成功，false表示失败
     */
    @Override
    public boolean delete(int id) {
        // 1.链接的获取
        Connection conn = DruidUtil.getConnection();
        // 2.预编译SQL语句
        PreparedStatement state = null;
        try {
            state = conn.prepareStatement(SQL_DELETE);
            state.setInt(1,id);
            return state.executeUpdate()>0?true:false;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            DruidUtil.close(conn,state,null);
        }
        return false;
    }

    /**
     * 通过数据库  查询每个人的快递总数与人名
     *
     * @return 返回快递总数与人名
     */
    @Override
    public List<Map<String, String>> findExpressRank() {
        List<Map<String,String>> list = new ArrayList<>();
        //获取数据库连接
        Connection conn = DruidUtil.getConnection();
        PreparedStatement state = null;
        ResultSet resultSet = null;
        //预编译SQL语句
        try {
            state = conn.prepareStatement(SQL_FINDALL_AND_NAME);
            //执行
            resultSet = state.executeQuery();
            while(resultSet.next()){
                String sum_express = String.valueOf(resultSet.getInt("sum_express"));
                String username = resultSet.getString("username");
                Map map = new HashMap();
                map.put("sum_express",sum_express);
                map.put("username",username);
                list.add(map);
            }

        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            //释放资源
            DruidUtil.close(conn,state,resultSet);
        }
        return list;
    }

    /**
     * 通过数据库  查询每个人当年的快递总数与人名
     *
     * @return 返回快递总数与人名
     */
    @Override
    public List<Map<String, String>> findExpressRankYear() {
        List<Map<String,String>> list = new ArrayList<>();
        //获取数据库连接
        Connection conn = DruidUtil.getConnection();
        PreparedStatement state = null;
        ResultSet resultSet = null;
        //预编译SQL语句
        try {
            state = conn.prepareStatement(SQL_FINDALL_AND_NAME_FROM_YEAR);
            //执行
            resultSet = state.executeQuery();
            while(resultSet.next()){
                String year_express = String.valueOf(resultSet.getInt("year_express"));
                String year_username = resultSet.getString("username");
                Map map = new HashMap();
                map.put("year_express",year_express);
                map.put("year_username",year_username);
                list.add(map);
            }

        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            //释放资源
            DruidUtil.close(conn,state,resultSet);
        }
        return list;
    }

    /**
     * 通过数据库  查询每个人当月的快递总数与人名
     *
     * @return 返回快递总数与人名
     */
    @Override
    public List<Map<String, String>> findExpressRankMonth() {
        List<Map<String,String>> list = new ArrayList<>();
        //获取数据库连接
        Connection conn = DruidUtil.getConnection();
        PreparedStatement state = null;
        ResultSet resultSet = null;
        //预编译SQL语句
        try {
            state = conn.prepareStatement(SQL_FINDALL_AND_NAME_FROM_MONTH);
            //执行
            resultSet = state.executeQuery();
            while(resultSet.next()){
                String month_express = String.valueOf(resultSet.getInt("month_express"));
                String month_username = resultSet.getString("username");
                Map map = new HashMap();
                map.put("month_express",month_express);
                map.put("month_username",month_username);
                list.add(map);
            }

        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            //释放资源
            DruidUtil.close(conn,state,resultSet);
        }
        return list;
    }
}
